/****************************************************************************************
* 
*   FILE        :    usb_terminal_commands.c
*   DATE CREATED:    
*   DESCRIPTION	:    
*
*   Author		:     
*  	Location	:    
*   Purpose		:
*   Copyright	:    
*                                                  
* UPDATED HISTORY:
*
* REV   YYYY.MM.DD  AUTHOR          	DESCRIPTION OF CHANGE
* ---   ----------  ------          	--------------------- 
* 1.0   2011.05.12  Gustavo Denardin	Initial version
*
****************************************************************************************/

/*
 * usb_terminal_commands.c
 *
 *  Created on: 12/05/2011
 *      Author: gustavo
 */

#include "usb_terminal.h"
#include "usb_terminal_commands.h"
#include "virtual_com.h"
#include "BRTOS.h"
#include "debug_stack.h"
#include "tasks.h"
#include "utils.h"

INT8U entradas[CONSOLE_BUFFER_SIZE]; //vetor para a entrada de dados



// BRTOS version Command
void usb_cmd_ver(char *param)
{
  (void)*param;
  printf_usb("\n\r");
  printf_usb((CHAR8*)version);
  printf_usb("\n\r");
}

const command_t usb_ver_cmd = {
  "ver", usb_cmd_ver, "BRTOS Version"
};



// TOP Command (similar to the linux command)
void usb_cmd_top(char *param)
{
  (void)*param;
  printf_usb("\n\r");
  Transmite_CPU_Load(USE_USB);
  Transmite_Uptime(USE_USB);
  Transmite_RAM_Ocupada(USE_USB);
  Transmite_Task_Stacks(USE_USB);
}

const command_t usb_top_cmd = {
  "top", usb_cmd_top, "BRTOS TOP"
};




// Print a string in the terminal
void cmd_echo(char *param)
{
	printSer(USE_USB, "\n\r");
	printSer(USE_USB, (CHAR8*)param);
}

const command_t echo_cmd = {
  "echo", cmd_echo, "Print the string in the terminal"
};


// Function used to print a string in the terminal by other tasks
void echo (char *string, char Terminalbackup)
{
    INT8U backup_cnt = 0;
    INT8U i = 0;
    char backup[(CONSOLE_BUFFER_SIZE/2)];
	char *command_start = "echo ";
    char *command_end   = "\n\r";
    
    if (Terminalbackup == TRUE)
    {
    	backup_cnt = TerminalBackup(&backup[0]);
    }
    
    SetSilentMode(TRUE);
    while (*command_start)
    {
			if (OSQueuePost(USB, *command_start++) == BUFFER_UNDERRUN)
			{
			  // Problema: Estouro de buffer
			  OSCleanQueue(USB);
			}
    }    
    
	while (*string)
    {
			if (OSQueuePost(USB, *string++) == BUFFER_UNDERRUN)
			{
			  // Problema: Estouro de buffer
			  OSCleanQueue(USB);
			}
    }
    
    while (*command_end)
    {
			if (OSQueuePost(USB, *command_end++) == BUFFER_UNDERRUN)
			{
			  // Problema: Estouro de buffer
			  OSCleanQueue(USB);
			}
    }
    
    if (Terminalbackup == TRUE)
    {
		for(i=0;i<backup_cnt;i++)
		{
			if (OSQueuePost(USB, backup[i]) == BUFFER_UNDERRUN)
			{
			  // Problema: Estouro de buffer
			  OSCleanQueue(USB);
			}    	
		}
    }
}



// Reason of Reset Command
void usb_cmd_rst(char *param)
{
  (void)*param;
  printf_usb("\n\r");
  Reason_of_Reset(USE_USB);
}

const command_t usb_rst_cmd = {
  "rst", usb_cmd_rst, "CPU Reason of the Reset"
};


#include <string.h>
#include "drivers/rgb.h"

// Enable/Disable RGB LED Task
void usb_cmd_rgbtask(char *param)
{
  if (strcmp (param, "on") == 0)
  {
	  UnBlockPriority(13);
	  printf_usb("\n\rRGB task enabled!\n\r");
  }else
  {
	  if (strcmp (param, "off") == 0)
	  {
		  BlockPriority(13);
		  printf_usb("\n\rRGB task disabled!\n\r");
	  }else
	  {
		  printf_usb("\n\rWrong parameter !\n\n\r");
		  printf_usb("Valid parameters are:\n\r");
		  printf_usb("on: Enable the RGB task!\n\r");
		  printf_usb("off: Disable the RGB task!\n\r");
	  }

  }
}

const command_t usb_rgbtask_cmd = {
  "rgbtask", usb_cmd_rgbtask, "Enable/Disable the RGB LED Task"
};

#define SUPPORTED_COLORS	5
const char *colors[SUPPORTED_COLORS]={"white", "red", "green", "blue", "yellow"};
// Changes RGB LED color
void usb_cmd_rgb(char *param)
{
	INT32U i = 0;
	volatile INT32U ulColor[3] = {0xFFFF, 0xFFFF, 0xFFFF};

	for(i=0;i<SUPPORTED_COLORS;i++)
	{
		if (strcmp (param, colors[i]) == 0) break;
	}

	if (i<SUPPORTED_COLORS)
	{
		switch(i)
		{
	   	   case 0:
	   		   ulColor[RED] = 32571;
	   		   ulColor[GREEN] = 39321;
	   		   ulColor[BLUE] = 0xFFFF;
	   		   RGBColorSet((volatile unsigned long *)&ulColor);
	   		   break;
	   	   case 1:
	   		   ulColor[RED] = 65535;
	   		   ulColor[GREEN] = 0;
	   		   ulColor[BLUE] = 0;
	   		   RGBColorSet((volatile unsigned long *)&ulColor);
	   		   break;
	   	   case 2:
	   		   ulColor[RED] = 0;
	   		   ulColor[GREEN] = 0xFFFF;
	   		   ulColor[BLUE] = 0;
	   		   RGBColorSet((volatile unsigned long *)&ulColor);
	   		   break;
	   	   case 3:
	   		   ulColor[RED] = 0;
	   		   ulColor[GREEN] = 0;
	   		   ulColor[BLUE] = 0xFFFF;
	   		   RGBColorSet((volatile unsigned long *)&ulColor);
	   		   break;
	   	   case 4:
	   		   ulColor[RED] = 32571;
	   		   ulColor[GREEN] = 39321;
	   		   ulColor[BLUE] = 0;
	   		   RGBColorSet((volatile unsigned long *)&ulColor);
	   		   break;
		}
	}else
	{
		  printf_usb("\n\rWrong color !\n\n\r");
		  printf_usb("Supported colors are: white, red, green, blue and yellow.\n\r");
	}
}

const command_t usb_rgb_cmd = {
  "rgb", usb_cmd_rgb, "Changes the RGB LED color"
};

